package com.haier.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haier.entity.HpTaskLog;
import com.haier.mapper.HpTaskLogMapper;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haier.constant.Constant;
import com.haier.constant.HpConstant;
import com.haier.constant.URLConstant;
import com.haier.service.IJobService;
import com.haier.utils.HttpUtils;

@Service
@Transactional(rollbackFor = Throwable.class)
public class JobServiceImpl implements IJobService {
	@Autowired
	private URLConstant urlConstant;
	@Autowired
	private HpTaskLogMapper hpTaskLogMapper;

	/**
	 * 生成待办
	 * @param taskId
	 * @param jobNumber 工号
	 * @param auditor 审核人
	 * @param title
	 * @param applyId
	 * @throws Exception
	 */
	@Override
	public void begin(String taskId, String jobNumber, String auditor, String title, Long applyId) throws Exception {
		JSONObject param = new JSONObject();
		param.put("complete", new JSONArray());
		JSONArray beginArray = new JSONArray();
		JSONObject begin = new JSONObject();
		begin.put("userid", auditor);//审核人
		begin.put("sysid", Constant.JOB_SYSID);//系统id
		begin.put("taskid", taskId);
		begin.put("title", title);//标题
		begin.put("initiator", jobNumber);//工号
		String taskviewurl=urlConstant.getPortalUrl() + applyId;//portal地址
		begin.put("taskviewurl", taskviewurl);
		String processdevice="1";
		begin.put("processdevice", processdevice);
		String mobileviewurl=urlConstant.getHipassUrl();//ihaier手机端地址
		begin.put("mobileviewurl", mobileviewurl);
		String pushdingding="0";
		begin.put("pushdingding", pushdingding);
		//String dingdingtodourl=urlConstant.getHipassUrl();
		String dingdingtodourl="";
		begin.put("dingdingtodourl", dingdingtodourl);
		beginArray.add(begin);
		param.put("begin", beginArray);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("info", param.toJSONString()));

		String result = HttpUtils.post(urlConstant.getJobUrl(), params, null);
		System.out.println(result);
		//记录日志
		HpTaskLog hpTaskLog = new HpTaskLog();
		hpTaskLog.setApplyId(applyId);
		hpTaskLog.setUserId(auditor);
		hpTaskLog.setSysid(Constant.JOB_SYSID);
		hpTaskLog.setTaskId(taskId);
		hpTaskLog.setTitle(title);
		hpTaskLog.setInitiator(jobNumber);
	    hpTaskLog.setTaskviewurl(taskviewurl);
	    hpTaskLog.setProcessdevice(processdevice);
	    hpTaskLog.setMobileviewurl(mobileviewurl);
	    hpTaskLog.setPushdingding(pushdingding);
	    hpTaskLog.setDingdingtodourl(dingdingtodourl);
	    hpTaskLog.setCreateTime(new Date());
	    hpTaskLog.setStatus(HpConstant.HP_GO_APPLY_STATUS_10);
		hpTaskLogMapper.insert(hpTaskLog);
	}

	/**
	 * 结束待办
	 * @param taskIdList
	 * @param auditor
	 * @param status
	 * @throws Exception
	 */
	@Override
	public void complete(List<String> taskIdList, String auditor, Integer status) throws Exception {
		JSONObject param = new JSONObject();
		param.put("begin", new JSONArray());
		String s = "";
		if (HpConstant.HP_GO_APPLY_STATUS_20.equals(status)) {//审核通过
			s = "1";
		} else if (HpConstant.HP_GO_APPLY_STATUS_F10.equals(status)) {//驳回
			s = "3";
		}else if(HpConstant.HP_GO_APPLY_STATUS_F20.equals(status)){//授权，撤销待办
			s = "2";
		}
		JSONArray completeArray = new JSONArray();
		int index = 0;
		for (int i = 0; i < taskIdList.size(); i++) {
			String taskId = taskIdList.get(i);
			JSONObject complete = new JSONObject();
			complete.put("userid", auditor);
			complete.put("status", s);
			complete.put("sysid", Constant.JOB_SYSID);
			complete.put("taskid", taskId);
			completeArray.add(complete);

			//更新待办日志状态
			QueryWrapper queryWrapper = new QueryWrapper();
			queryWrapper.eq("task_id",taskId);
			queryWrapper.eq("status",HpConstant.HP_GO_APPLY_STATUS_10);
			HpTaskLog hpTaskLog = hpTaskLogMapper.selectOne(queryWrapper);
			hpTaskLog.setStatus(status);
			hpTaskLogMapper.updateById(hpTaskLog);

			if (index++ > 95) {
				param.put("complete", completeArray);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("info", param.toJSONString()));
				String result = HttpUtils.post(urlConstant.getJobUrl(), params, null);
				System.out.println(result);
				completeArray = new JSONArray();
				index = 0;
			}
		}
		if (index > 0) {
			param.put("complete", completeArray);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("info", param.toJSONString()));
			String result = HttpUtils.post(urlConstant.getJobUrl(), params, null);
			System.out.println(result);
		}
	}
}
